package servlet.merchant.api;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import dto.StatusDto;
import pojo.MerchantApplication;
import service.MerchantApplicationManager;
import service.impl.MerchantApplicationManagerImpl;
import util.GetMerchantStatusUtil;
import util.JsonUtil;

public class GetMerchantStatusServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	private MerchantApplicationManager amanager = null;
	
    public GetMerchantStatusServlet() {
        super();
        amanager = new MerchantApplicationManagerImpl();
    }

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		response.setCharacterEncoding("UTF-8");
		response.setHeader("Content-type", "application/json;charset=UTF-8");
		String name = request.getParameter("name");
		MerchantApplication mApplication = amanager.getByName(name);
		PrintWriter writer = response.getWriter();
		String status = null;
		StatusDto statusDto = new StatusDto();
		if(mApplication != null){
			status = mApplication.getStatus();
			statusDto.setMessage(mApplication.getRejectReason());
		}
		String merchantStatus = GetMerchantStatusUtil.getStatus(status);
		statusDto.setStatus(merchantStatus);
		System.out.println(JsonUtil.toJsonString(statusDto));
		writer.write(JsonUtil.toJsonString(statusDto));
		writer.close();
	
	}
}
